package com.cy.aircompressor.main.ui.dataanalysis.provider;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.cy.aircompressor.databinding.ItemSensorDataAnalysisBinding;
import com.cy.aircompressor.entity.sensor.SensorDataAnalysis;
import com.cy.aircompressor.entity.sensor.SensorStatisticsData;
import com.cy.aircompressor.main.ui.onlinemonitor.utils.LineChartUtil;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.github.mikephil.charting.data.Entry;
import com.net.model.base.BaseBindViewHolder;

import java.util.ArrayList;
import java.util.List;

public class SensorDataAnalysisProvider extends ItemViewBinder<SensorDataAnalysis, SensorDataAnalysisProvider.ViewHolder> {
    Context mContext;

    public SensorDataAnalysisProvider(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    protected SensorDataAnalysisProvider.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemSensorDataAnalysisBinding.inflate(inflater, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull SensorDataAnalysisProvider.ViewHolder holder, @NonNull SensorDataAnalysis entity) {
        holder.mBinding.partsName.setText(entity.getPartName());
//        List<Entry> temperatures = getChartEntry(entity.getTemperatureSStatisticsData());
//        if (entity.getTemperatureSensor() != null)
//            holder.mBinding.sensorName1.setText("温度变化趋势");
//        LineChartUtil.showChart(holder.mBinding.temperatureChart, temperatures);
//
//
//        List<Entry> pressures = getChartEntry(entity.getPressureStatisticsData());
//        if (entity.getPressureSensor() != null)
//            holder.mBinding.sensorName2.setText("压力变化趋势");
//        LineChartUtil.showChart(holder.mBinding.pressureChart, pressures);
//
//
//        List<Entry> waterPressures = getChartEntry(entity.getWaterPressureStatisticsData());
//        if (entity.getWaterPressureSensor() != null)
//            holder.mBinding.sensorName1.setText("水压变化趋势");
//        LineChartUtil.showChart(holder.mBinding.temperatureChart, waterPressures);
//
//        List<Entry> oilPressures = getChartEntry(entity.getOilPressureStatisticsData());
//        if (entity.getOilPressureSensor() != null)
//            holder.mBinding.sensorName2.setText("油压变化趋势");
//        LineChartUtil.showChart(holder.mBinding.pressureChart, oilPressures);
    }

    private List<Entry> getChartEntry(List<SensorStatisticsData> statisticsData) {
        List<Entry> entries = new ArrayList<>();
        if (statisticsData != null && statisticsData.size() != 0) {
            for (int i = 0; i < statisticsData.size(); i++) {
                SensorStatisticsData sensorStatisticsData = statisticsData.get(i);
                Entry entry = new Entry(i, (float) sensorStatisticsData.getV(), sensorStatisticsData.getRt());
                entries.add(entry);
            }
        }
        return entries;
    }

    public class ViewHolder extends BaseBindViewHolder<ItemSensorDataAnalysisBinding> {

        protected ViewHolder(ItemSensorDataAnalysisBinding itemView) {
            super(itemView);
            LineChartUtil.initLineChart(mBinding.lineChart, false);
            LineChartUtil.initLineChart(mBinding.lineChart2, false);
        }
    }


}
